//
//  MyPictureVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/29.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "MyPictureVC.h"
#import "AlumPicCollectionViewCell.h"
#import "PictureModel.h"
#import "AlumContentViewController.h"
#import "LoginViewController.h"
@interface MyPictureVC ()
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>
{
    UICollectionView * _collectionView;
    NSMutableArray * dataSource;
    
    CGSize size;
}
@property (strong, nonatomic) FeRollingLoader *rollingLoader;
@end

@implementation MyPictureVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app.viewController isHiddenCustomTabBarByBoolean:YES];
//    self.navigationController.navigationBarHidden = NO;
    [self requestData];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    
    _rollingLoader = [[FeRollingLoader alloc] initWithView:self.view title:@"LOADING"];
    [self.view addSubview:_rollingLoader];
    [_rollingLoader show];

    
    [self addrightItem];
    
    self.navigationItem.title =@"我的图片库";
   // [self addNavgationBar];
    [self addBackItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createAlumeNotificase:) name:@"createAlumeNotificase" object:nil];
    CGFloat picWidth = (SCREEN_WIDTH - SIZETO6PlusW(15)*2-SIZETO6W(6)*2)/3;
    size = CGSizeMake(picWidth, picWidth+20);
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumInteritemSpacing = SIZETO6W(6);
    flow.minimumLineSpacing = SIZETO6W(6);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.sectionInset = UIEdgeInsetsMake(SIZETO6W(6), SIZETO6W(6), SIZETO6W(6), SIZETO6W(6));
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[AlumPicCollectionViewCell class] forCellWithReuseIdentifier:@"AlumPicCollectionViewCell"];
    [self.view addSubview:_collectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    
    
    
    
    
    
    
    [self.view setBackgroundColor:UIColorFromRGB(0xf1f3f2)];
//    UIImageView * bgImgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(122), SIZETO6H(187), SCREEN_WIDTH-SIZETO6W(122)*2, SCREEN_WIDTH-SIZETO6W(122)*2)];
//    bgImgV.image = [UIImage imageNamed:@"mypicture－添加照片"];
//    [self.view addSubview:bgImgV];
}
-(void)requestData
{

    [U76HONet minePictureWithaction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        if (arr.count == 0) {
            UIView * view = (UIView *)[self.view viewWithTag:504];
            if (view) {
                [view removeFromSuperview];
            }
            [self nosearch];
            [_rollingLoader removeFromSuperview];
        }
        else
        {
            UIView * view = (UIView *)[self.view viewWithTag:504];
            if (view) {
                [view removeFromSuperview];
            }
            if (dataSource.count!=0) {
                [dataSource removeAllObjects];
            }
            [dataSource addObjectsFromArray:arr];
            [_collectionView reloadData];
            [_rollingLoader removeFromSuperview];

            NSLog(@"获取我的图片库数据成功了");
        }

    }];
}
-(void)nosearch
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.tag = 504;
    
    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-98/2,SIZETO6H(120), 98, 100)];
    imgV.image = [UIImage imageNamed:@"苦脸"];
    [view addSubview:imgV];
    
    UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, imgV.bottom+SIZETO6H(20), SCREEN_WIDTH, 20)];
    tipLab.text = @"暂时没有符合条件的内容";
    tipLab.textAlignment = NSTextAlignmentCenter;
    tipLab.font = [UIFont systemFontOfSize:14];
    tipLab.textColor = UIColorFromRGB(0x333944);
    [view addSubview:tipLab];
    [self.view addSubview:view];
}

-(void)createAlumWithname:(NSString *)name
{
    __weak typeof(self)weakSelf = self;
    [U76HONet createAlumWithaction:@"" code:[Singleton share].code name:name andDataBlock:^(NSDictionary *dic) {
        NSLog(@"创建相册成功了");
        [weakSelf requestData];
    }];
}
#pragma mark - _collectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlumPicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlumPicCollectionViewCell" forIndexPath:indexPath];
    cell.model = dataSource[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureModel * model = [dataSource objectAtIndex:indexPath.row];
    NSLog(@"id 是%@",model.ids);
    AlumContentViewController * contentVC = [[AlumContentViewController alloc]init];
    contentVC.alumId = model.ids;
    contentVC.alumName = model.name;
    [self.navigationController pushViewController:contentVC animated:YES];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
}

#pragma mark - rightItems
-(void)addrightItem
{
    UIButton * createBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    createBtn.frame = CGRectMake(0, 0, 80, 30);
    [createBtn setTitle:@"创建相册" forState:UIControlStateNormal];
    [createBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [createBtn addTarget:self action:@selector(createBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:createBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    if (_isCreate == YES) {
        [self performSelector:@selector(createBtnAction:) withObject:nil afterDelay:0.1];
    }
}
-(void)createBtnAction:(id)sender
{
    if ([self isLogin]) {
        [Singleton alercreateAlumeMessage:@"创建相册" content:@"请为次此相册输入名称" btnName:@"确定"];
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                [self presentViewController:nvc animated:YES completion:nil];
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}
#pragma mark - 消息通知的实现方法
-(void)createAlumeNotificase:(NSNotification *)noti
{
    NSLog(@"我恩图片库监听到了通知");
    NSLog(@"%@",noti.object);
    if (noti.object != nil) {
        [self createAlumWithname:noti.object];
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - NAVgationBarTitle


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
